<template>
  <div class="app-views">
    <div
      class="item-box"
      v-loading="loading"
      style="border-right: 1px dashed #e6e6e6"
    >
      <div class="sub-title">
        <i
          class="iconfont icon-list"
          style="margin-right: 5px; font-weight: bold"
        ></i>
        <div class="Title">设备列表</div>
      </div>
      <div class="device-list">
        <DCard
          :dName="item.name"
          :isCheck="isCheckCard == item.value"
          @click.native="handlerisCheck(item)"
          v-for="(item, index) in cage"
          :key="index"
          :status="deviceList[index]"
          :use="isUse[index]"
        />
      </div>
    </div>
    <div class="device-views">
      <div class="sub-title">
        <i class="iconfont icon-shebeituceng-" style="margin-right: 5px"></i>
        <div class="Title">设备图</div>
      </div>
      <div class="device-img">
        <div class="device-img-rec">
          <div class="img-box">
            <img src="@/assets/images/cage.png" alt="" />
          </div>
          <div class="deviceStatus">
            <div class="title">
              设备状态
              <i
                class="el-icon-edit"
                @click="openParamSet"
                v-hasPermi="['assembly:device:edit']"
              ></i>
              <i
                class="el-icon-c-scale-to-original"
                v-hasPermi="['assembly:device:edit']"
                @click="openUnderCatSet"
              ></i>
            </div>
            <div class="list" v-loading="showDeviceStatus.length == 0">
              <div class="item" v-for="item in showDeviceStatus" :key="item.id">
                <div class="descript">
                  <span class="note"> {{ item.note }}</span>
                  <span class="value">
                    {{ item.message }}
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="signal">
          <div class="sub-title">
            <i
              class="iconfont icon-PLCxinhaoganzhizhongduan"
              style="margin-right: 5px"
            ></i>
            <div class="Title">信号列表</div>
          </div>
          <div class="signal-table">
            <div class="signal-table">
              <i
                class="iconfont icon-jinyong"
                style="marginright: 5px; fontsize: 30px"
              ></i>
              该设备无plc信号
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="item-box">
      <LogView :jobGroup="6" :isShow="isShow" />
    </div>
    <el-drawer title="设备参数" :visible.sync="drawer" :direction="direction">
      <lineSet
        :drawer="
          () => {
            drawer = false;
          }
        "
      />
    </el-drawer>
    <el-drawer
      title="设备参数"
      :visible.sync="drawerCat"
      :direction="direction"
    >
      <div
        class="params"
        v-if="underCatParams != null"
        v-loading="loadDeviceParam"
      >
        <div class="select">
          <div
            class="underCat"
            v-for="(item, index) in underCat"
            :key="item.id"
            :class="selectCat == index ? 'active' : null"
            @click="handlerSelect(index)"
          >
            {{ item.name }}
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">小车是否使用</div>
          <div class="paramView">
            <el-radio-group v-model="underCatParams.object.use">
              <el-radio :label="true">开启</el-radio>
              <el-radio :label="false">关闭</el-radio>
            </el-radio-group>
          </div>
        </div>
        <div class="param_item_W">
          <div class="paramsName">安全位置</div>
          <div class="paramView">
            <span
              v-show="SafeLocation"
              @click.stop="handlerSpace('SafeLocation')"
              >{{ underCatParams.object.safeLocation }}</span
            >
            <el-input-number
              v-show="!SafeLocation"
              size="mini"
              ref="SafeLocation"
              v-model="underCatParams.object.safeLocation"
              @blur="handlerSpace('SafeLocation')"
            ></el-input-number>
          </div>
        </div>

        <div class="param_item_W gridBox">
          <div class="paramsName gridHeader">
            <div>使用格号</div>
            <div style="display: flex; align-items: center; margin-left: auto">
              <div
                style="
                  width: 10px;
                  height: 10px;
                  background-color: #409eff;
                  margin-right: 3px;
                "
              ></div>
              使用
            </div>
            <div style="display: flex; align-items: center; margin-left: 5px">
              <div
                style="
                  width: 10px;
                  height: 10px;
                  background-color: #a6a9ad;
                  margin-right: 3px;
                "
              ></div>
              禁用
            </div>
          </div>
          <div class="checkBoxGroup">
            <el-checkbox-group v-model="checkList">
              <el-checkbox
                v-if="selectCat == 0 || selectCat == 1"
                :indeterminate="
                  checkFillArrayExit(50, underCatParams.object.canUseCell) !=
                    0 &&
                  checkFillArrayExit(50, underCatParams.object.canUseCell) != 50
                "
                :label="50"
                >1-50</el-checkbox
              >
              <el-checkbox
                :indeterminate="
                  checkFillArrayExit(100, underCatParams.object.canUseCell) !=
                    0 &&
                  checkFillArrayExit(100, underCatParams.object.canUseCell) !=
                    50
                "
                :label="100"
                >51-100</el-checkbox
              >
              <el-checkbox
                :indeterminate="
                  checkFillArrayExit(150, underCatParams.object.canUseCell) !=
                    0 &&
                  checkFillArrayExit(150, underCatParams.object.canUseCell) !=
                    50
                "
                :label="150"
                >101-150</el-checkbox
              >
              <el-checkbox
                :indeterminate="
                  checkFillArrayExit(200, underCatParams.object.canUseCell) !=
                    0 &&
                  checkFillArrayExit(200, underCatParams.object.canUseCell) !=
                    50
                "
                :label="200"
                >151-200</el-checkbox
              >
              <el-checkbox
                :indeterminate="
                  checkFillArrayExit(250, underCatParams.object.canUseCell) !=
                    0 &&
                  checkFillArrayExit(250, underCatParams.object.canUseCell) !=
                    50
                "
                :label="250"
                >201-250</el-checkbox
              >
              <el-checkbox
                :indeterminate="
                  checkFillArrayExit(300, underCatParams.object.canUseCell) !=
                    0 &&
                  checkFillArrayExit(300, underCatParams.object.canUseCell) !=
                    50
                "
                :label="300"
                >251-300</el-checkbox
              >
              <el-checkbox
                v-if="selectCat == 2 || selectCat == 3"
                :indeterminate="
                  checkFillArrayExit(300, underCatParams.object.canUseCell) !=
                    0 &&
                  checkFillArrayExit(300, underCatParams.object.canUseCell) !=
                    50
                "
                :label="350"
                >301-350</el-checkbox
              >
            </el-checkbox-group>
          </div>
          <div class="paramView">
            <div
              class="grid"
              v-for="item in gridRange[selectCat]"
              :key="item"
              :class="
                underCatParams.object.canUseCell.includes(item)
                  ? 'gridActive'
                  : ''
              "
              @click="handlerCheckGrid(item)"
            >
              {{ item }}
            </div>
          </div>
        </div>

        <div class="footer">
          <el-button @click="drawerCat = false">取消</el-button>
          <el-button type="primary" @click="handlerEditDevice('underCatParams')"
            >确定</el-button
          >
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  comparabelArray,
  checkFillArrayExit,
  intervalNumber,
} from "@/utils/index";
import DCard from "@/views/components/production/dCard.vue";
import LogView from "@/views/components/production/logViews.vue";
import { cage, underCat } from "../Config/label";
import mixin from "../mixin/index";
import { cageDB } from "@/config/db";
import { mapState } from "vuex";
import { deviceEditParam } from "@/api/assembly/device";
import lineSet from "../../operation/components/lineSet.vue";
export default {
  components: {
    DCard,
    LogView,
    lineSet,
  },
  props: {},
  mixins: [mixin],
  data() {
    return {
      underCat,
      cage,
      isCheckCard: 0,
      loading: false,
      isShow: false,
      tableData: [],
      device: null,
      drawer: false,
      drawerCat: false,
      deviceParams: null,
      underCatParams: null,
      direction: "rtl",
      loadDeviceParam: true,
      SafeLocation: true,

      Width: true,
      selectCat: 0,
      isUse: [216, 272, 328, 384],
      checkList: [],
    };
  },
  watch: {
    checkList(newValue, oldValue) {
      const arr = comparabelArray(oldValue, newValue);
      const isIncrease = oldValue.length > newValue.length ? false : true;
      if (isIncrease) {
        for (let i = arr[0]; i > arr[0] - 50; i--) {
          if (!this.underCatParams.object.canUseCell.includes(i)) {
            this.underCatParams.object.canUseCell.push(i);
          }
        }
      } else {
        for (let i = arr[0]; i > arr[0] - 50; i--) {
          let indexOf = this.underCatParams.object.canUseCell.indexOf(i);
          indexOf != -1
            ? this.underCatParams.object.canUseCell.splice(indexOf, 1)
            : null;
        }
      }
    },
  },
  methods: {
    handlerCheckGrid(item) {
      let canUseList = this.underCatParams.object.canUseCell;
      let indexOf = canUseList.indexOf(item);
      indexOf == -1 ? canUseList.push(item) : canUseList.splice(indexOf, 1);
      this.handlerCheckisValue(item);
    },
    handlerInitCheckBox() {
      let a = [50, 100, 150, 200, 250, 300, 350, 400];
      a.forEach((item) => {
        this.handlerCheckisValue(item);
      });
    },
    handlerCheckisValue(item) {
      let canUseList = this.underCatParams.object.canUseCell;
      let num = intervalNumber(item);
      let total = checkFillArrayExit(num, canUseList);
      if (total == 50) {
        this.checkList.push(num);
      }
      if (total == 0) {
        let indexOf = this.checkList.indexOf(num);
        indexOf != -1 && this.checkList.splice(this.checkList.indexOf(num), 1);
      }
    },
    checkFillArrayExit,
    async handlerSelect(e) {
      this.selectCat = e;
      await this.getUnderCat();
      this.checkList = [];
      this.handlerInitCheckBox();
    },
    openUnderCatSet() {
      this.drawerCat = true;
      this.getUnderCat();
      this.checkList = [];
    },
    async getUnderCat() {
      this.loadDeviceParam = true;
      let paramList = this.underCat.map((item) => {
        return {
          name: item.deviceType,
          id: item.id,
        };
      });
      await this.$store.dispatch("getDeviceMap", paramList);
      this.underCatParams =
        this.deviceParamsList.length == 0
          ? null
          : JSON.parse(JSON.stringify(this.deviceParamsList))[this.selectCat];
      this.handlerInitCheckBox();
      this.loadDeviceParam = false;
    },
    handlerSpace(e) {
      this[e] = !this[e];
      this.$nextTick(() => {
        !this[e] && this.$refs[e].focus();
      });
    },
    handlerEditDevice(e) {
      this.$prompt("请输入密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^5563$/,
        inputErrorMessage: "密码错误",
        inputType: "password",
      })
        .then(({ value }) => {
          deviceEditParam(this[e]).then((res) => {
            if (res.code == 200) {
              this.$message.success("修改设备状态成功");
              this.drawer = false;
              this.drawerCat = false;
              this.initData();
            } else {
              this.$message.error("修改设备状态失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    // index 选中的card
    handlerisCheck(item) {
      this.isCheckCard = item.value;
      this.device = item;
      this.getDeviceSignal();
    },
    async openParamSet() {
      this.drawer = true;
      this.initData();
    },
    async initData() {
      this.loadDeviceParam = true;
      let paramList = this.cage.map((item) => {
        return {
          name: item.deviceType,
          id: item.id,
        };
      });
      await this.$store.dispatch("getDeviceMap", paramList);

      this.deviceParams =
        this.deviceParamsList.length == 0
          ? null
          : this.deviceParamsList[this.isCheckCard];
      this.loadDeviceParam = false;
    },
  },
  created() {
    // this.device = cage[this.isCheckCard];
    this.getSignal(cageDB);
  },
  destroyed() {},
  computed: {
    ...mapState({
      deviceParamsList: (state) => state.deviceStatus.deviceParams,
    }),
    showDeviceStatus() {
      if (this.deviceList.length == 0) return [];
      let blockAddr = [[0, 54, 108, 162]];
      return this.deviceList[this.isCheckCard].plcSignals.filter(
        (item) =>
          item.block == cageDB[this.isCheckCard].block &&
          blockAddr[this.isCheckCard].includes(item.addr)
      );
    },
    isDisable() {
      if (this.isCheckCard == 2 || this.isCheckCard == 3) return true;
      return false;
    },
    isDisableA27() {
      if (this.isCheckCard == 2) return true;
      return false;
    },
    isDisableGLST() {
      if (this.isCheckCard == 3) return true;
      return false;
    },
    gridRange() {
      let gridArr = [[], [], [], []];
      let map = {
        0: [1, 300],
        1: [1, 300],
        2: [51, 350],
        3: [51, 350],
      };
      for (let key in map) {
        for (let i = map[key][0]; i <= map[key][1]; i++) {
          gridArr[key].push(i);
        }
      }
      return gridArr;
    },
  },
};
</script>

<style lang="less" scoped>
@import url("../style/index.less");

/deep/ .el-checkbox {
  margin-right: 15px;
}
img {
  width: 45%;
  height: 99%;
}
.select {
  min-height: 40px;
  display: flex;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 5%);
  cursor: pointer;
  .underCat {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: bold;
  }

  .active {
    background: linear-gradient(#fafafa, #1890ff);
  }
}
.select {
  min-height: 40px;
  display: flex;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 5%);
  cursor: pointer;
  .underCat {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: bold;
  }

  .active {
    background: linear-gradient(#fafafa, #1890ff);
  }
}

.params {
  display: flex;
  flex-direction: column;
  height: 100%;

  .paramsName {
    min-height: 30px;
    padding: 0px 10px;
    display: flex;
    font-size: 16px;
    align-items: center;
  }

  .paramView {
    min-height: 40px;
    max-width: 100%;
    display: flex;
    align-items: center;
    padding: 0px 0px 0px 10px !important;
    margin-left: auto;
    flex-wrap: wrap;

    span {
      cursor: pointer;
    }

    .grid {
      width: 5.15%;
      height: 30px;
      background-color: #a6a9ad;
      color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-top: 5px;
      margin-right: 4px;
      cursor: pointer;
      font-size: 14px;
    }

    .gridActive {
      background-color: #409eff;
    }
  }
  .gridBox {
    flex-direction: column;
  }
  .gridHeader {
    display: flex;
    flex-wrap: wrap;
  }
  .checkBoxGroup {
    padding: 0px 10px;
    display: flex;
  }
}
.signal {
  flex: 1;
  overflow: hidden;
  .signal-table {
    height: 100%;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    color: #e6e6e6;
  }
}
</style>
